#if !defined( BINARY_IO_H)
#define  BINARY_IO_H
#include <iostream>
#include <string>
namespace binary_io
{
template<typename T> inline std::ostream& binary_write(std::ostream& os, T t)
{
	os.write(reinterpret_cast<char*>(&t), sizeof(T));
	return os;
}

inline std::ostream& binary_write(std::ostream& os, const std::string& str)
{
	binary_write<unsigned>(os, str.length());
	os.write(str.c_str(), str.length());
	return os;
}

template<typename T> inline T binary_read(std::istream& is)
{
	T t;
	is.read(reinterpret_cast<char*>(&t), sizeof(T));
	return t;
}

template<> inline std::string binary_read(std::istream& is)
{
	unsigned len = binary_read<unsigned>(is);
	char* buf = new char[len + 1];
	is.read(buf, len);
	return std::string(buf, len);
}
}
#endif	//BINARY_IO_H

